<?php

/*
Plugin Name: FIFA World Cup South Africa scoreboard.
Description: Get the latest results of 2010 FIFA World Cup South Africa™. Use [wp_fifa_world_cup_scoreboard] into a post or a page and/or use the sidebar widget.
Plugin URI: http://nomikos.info/2010/06/10/fifa-world-cup-south-africa-scoreboard-wp-plugin.html
Version: 0.1
Author: NomikOS
Author URI: http://www.rentacoder.com/RentACoder/DotNet/SoftwareCoders/ShowBioInfo.aspx?lngAuthorId=7064234
*/

define( 'NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_DIR', WP_PLUGIN_DIR . '/wp-fifa-world-cup-scoreboard' );
define( 'NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_URL', plugins_url( $path = '/wp-fifa-world-cup-scoreboard' ) );

class nomikos_fifa_world_cup_scoreboard_class
{
    function nomikos_fifa_world_cup_scoreboard_class()
    {
        $this->feed_url     = 'http://www.fifa.com/worldcup/matches/index.html';
        $this->current_time = time() + get_option('gmt_offset');
        $this->results      = array();
    }

    # call only initialited from shortcode or widget
    # ----------------------------------------------
    function init()
    {
        $this->options = get_option('nomikos_fifa_world_cup_scoreboard_options');
        $this->options->show_results->match = 1;
        $this->options->show_results->date  = 1;

        # get feed each 5 minutes at the most
        if ($this->options->last_feed < $this->current_time - 5 * 60)
        {
            $this->options->last_feed = $this->current_time;

            # get feed
            # --------
            include (NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_DIR . '/php/scrape.php');

            $this->options->results = $results;
            update_option('nomikos_fifa_world_cup_scoreboard_options', $this->options);
        }
    }

    function draw_page()
    {
        $this->init();

        include (NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_DIR . '/templates/page.tpl');
    }

    function header_scripts()
    {
    }

    function header_styles()
    {
        wp_enqueue_style('nomikos_fifa_world_cup_scoreboard_style_common', NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_URL.'/css/common.css');
    }

    function error($log = '')
    {
        $debug = strstr($_SERVER['SERVER_ADMIN'], '@nomikos.info') ? 1 : 0;
        
        $c = date('c', time());
        $bt = debug_backtrace();
        $output  = "[log $c\n";
        $output .= 'file : ' . $bt[0]['file'] . "\n";
        $output .= 'line : ' . $bt[0]['line'] . "\n";
        $output .= 'log  : ' . $log . "\n";

        if ($debug)
        {
            echo str_replace("\n", "<br />\n", $output);
        }
        else
        {
            $fp = fopen(NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_DIR . '/log/log.txt', 'a');
            fwrite($fp, $output);
            fclose($fp);
        }
    }

    # plugin install && uninstall
    # ---------------------------
    function install()
    {
    }

    function uninstall()
    {
        delete_option('nomikos_fifa_world_cup_scoreboard_options');
    }
}

# widget stuff
# ------------

function nomikos_fifa_world_cup_scoreboard_widget_manual($args = null)
{
    global $nomikos_fifa_world_cup_scoreboard_class;

    $nomikos_fifa_world_cup_scoreboard_class->init();

    if ( is_null($args) )
    {
        $random = rand(1, 8);
        $new_instance['title']   = '';
        $new_instance['date']    = 1;
        $new_instance['one_row'] = 0;

        for ( $i = 0; $i < 8; $i++ )
        {
            $gletter = chr($i + 65);
            if ( $i == $random )
                $new_instance[$gletter] = 1;
            else
                $new_instance[$gletter] = 0;
        }
    }
    else
    {
        $atts = explode( '&', trim( $args ) );
        foreach ($atts as $att)
        {
            $at = explode( '=', trim( $att ) );
            $new_instance[strtolower($at[0])] = $at[1] ? 1 : 0;
        }

        $new_instance['title']   = '';
    }

    $instance['title']   = htmlspecialchars(strip_tags(trim($new_instance['title'])));
    $instance['date']    = $new_instance['date']    ? 1 : 0;
    $instance['one_row'] = $new_instance['one_row'] ? 1 : 0;
    $instance['a']       = $new_instance['a'] ? $new_instance['a'] : 0;
    $instance['b']       = $new_instance['b'] ? $new_instance['b'] : 0;
    $instance['c']       = $new_instance['c'] ? $new_instance['c'] : 0;
    $instance['d']       = $new_instance['d'] ? $new_instance['d'] : 0;
    $instance['e']       = $new_instance['e'] ? $new_instance['e'] : 0;
    $instance['f']       = $new_instance['f'] ? $new_instance['f'] : 0;
    $instance['g']       = $new_instance['g'] ? $new_instance['g'] : 0;
    $instance['h']       = $new_instance['h'] ? $new_instance['h'] : 0;

    include (NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_DIR . '/templates/sidebar.tpl');
}

class nomikos_fifa_world_cup_scoreboard_widget_init extends wp_widget
{
    function nomikos_fifa_world_cup_scoreboard_widget_init()
    {
        $widget_ops = array('classname' => 'nomikos_fifa_world_cup_scoreboard_widget', 'description' => __( 'Get the latest results of 2010 FIFA World Cup South Africa™', 'nomikos_fifa_world_cup_scoreboard_widget2') );
        $this->WP_Widget('nomikos_fifa_world_cup_scoreboard-widget', __('FIFA World Cup', 'nomikos_fifa_world_cup_scoreboard_widget2'), $widget_ops);
    }

    function widget($args, $instance)
    {
        global $nomikos_fifa_world_cup_scoreboard_class;

        $nomikos_fifa_world_cup_scoreboard_class->init();

        extract($args);
        $title = $instance['title'] ? apply_filters('widget_title', $instance['title']) : '';

        echo $before_widget;
        echo $before_title . $title . $after_title;
        include (NOMIKOS_FIFA_WORLD_CUP_SCOREBOARD_PLUGIN_DIR . '/templates/sidebar.tpl');
        echo $after_widget;
    }

    function update( $new_instance, $old_instance )
    {
        $instance = $old_instance;

        $instance['title']   = strip_tags($new_instance['title']);
        $instance['date']    = $new_instance['date']    ? 1 : 0;
        $instance['one_row'] = $new_instance['one_row'] ? 1 : 0;
        $instance['a']       = $new_instance['a'] ? $new_instance['a'] : 0;
        $instance['b']       = $new_instance['b'] ? $new_instance['b'] : 0;
        $instance['c']       = $new_instance['c'] ? $new_instance['c'] : 0;
        $instance['d']       = $new_instance['d'] ? $new_instance['d'] : 0;
        $instance['e']       = $new_instance['e'] ? $new_instance['e'] : 0;
        $instance['f']       = $new_instance['f'] ? $new_instance['f'] : 0;
        $instance['g']       = $new_instance['g'] ? $new_instance['g'] : 0;
        $instance['h']       = $new_instance['h'] ? $new_instance['h'] : 0;

        return $instance;
    }

    function form( $instance )
    {
        $title = attribute_escape($instance['title']);
    ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>">Title:<i> (not required)</i></p>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <p></p>
        <p>Select groups:<i></p>
        <input id="<?php echo $this->get_field_id('a'); ?>" name="<?php echo $this->get_field_name('a'); ?>" class="checkbox" type="checkbox" <?php echo $instance['a'] ? 'checked' : '' ?>/> A </label><br />
        <input id="<?php echo $this->get_field_id('b'); ?>" name="<?php echo $this->get_field_name('b'); ?>" class="checkbox" type="checkbox" <?php echo $instance['b'] ? 'checked' : '' ?>/> B </label><br />
        <input id="<?php echo $this->get_field_id('c'); ?>" name="<?php echo $this->get_field_name('c'); ?>" class="checkbox" type="checkbox" <?php echo $instance['c'] ? 'checked' : '' ?>/> C </label><br />
        <input id="<?php echo $this->get_field_id('d'); ?>" name="<?php echo $this->get_field_name('d'); ?>" class="checkbox" type="checkbox" <?php echo $instance['d'] ? 'checked' : '' ?>/> D </label><br />
        <input id="<?php echo $this->get_field_id('e'); ?>" name="<?php echo $this->get_field_name('e'); ?>" class="checkbox" type="checkbox" <?php echo $instance['e'] ? 'checked' : '' ?>/> E </label><br />
        <input id="<?php echo $this->get_field_id('f'); ?>" name="<?php echo $this->get_field_name('f'); ?>" class="checkbox" type="checkbox" <?php echo $instance['f'] ? 'checked' : '' ?>/> F </label><br />
        <input id="<?php echo $this->get_field_id('g'); ?>" name="<?php echo $this->get_field_name('g'); ?>" class="checkbox" type="checkbox" <?php echo $instance['g'] ? 'checked' : '' ?>/> G </label><br />
        <input id="<?php echo $this->get_field_id('h'); ?>" name="<?php echo $this->get_field_name('h'); ?>" class="checkbox" type="checkbox" <?php echo $instance['h'] ? 'checked' : '' ?>/> H </label><br />
        <p></p>
        <input id="<?php echo $this->get_field_id('one_row'); ?>" name="<?php echo $this->get_field_name('one_row'); ?>" class="checkbox" type="checkbox" <?php echo $instance['one_row'] ? 'checked' : '' ?>/> Only one row by match </label><br />
        <p></p>
        <input id="<?php echo $this->get_field_id('date'); ?>" name="<?php echo $this->get_field_name('date'); ?>" class="checkbox" type="checkbox" <?php echo $instance['date'] ? 'checked' : '' ?>/> Show date </label><br />
    <?php
    }
}

add_action('widgets_init', 'nomikos_fifa_world_cup_scoreboard_widget');
function nomikos_fifa_world_cup_scoreboard_widget()
{
    register_widget('nomikos_fifa_world_cup_scoreboard_widget_init');
}

$nomikos_fifa_world_cup_scoreboard_class = new nomikos_fifa_world_cup_scoreboard_class();

if ($nomikos_fifa_world_cup_scoreboard_class)
{
    register_activation_hook(__file__, array($nomikos_fifa_world_cup_scoreboard_class, 'install'));
    register_deactivation_hook(__file__, array($nomikos_fifa_world_cup_scoreboard_class, 'uninstall'));

    add_action('template_redirect', array($nomikos_fifa_world_cup_scoreboard_class, 'header_scripts'));
    add_action('template_redirect', array($nomikos_fifa_world_cup_scoreboard_class, 'header_styles'));
    add_shortcode('wp_fifa_world_cup_scoreboard', array($nomikos_fifa_world_cup_scoreboard_class, 'draw_page'));
}

if ( ! function_exists('d') )
{
    function d($var,  $exit = 0)
    {
        echo "\n\n\n<hr />[nomikos_degug_output_BEGIN]\n<pre>" . var_export($var, 1) . "\n</pre>[nomikos_degug_output_END]<hr />\n\n\n";
        if ($exit)
            exit;
    }
}
?>
